package cn.xz.zgsg.dialog

import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import cn.xz.zgsg.R
import cn.xz.zgsg.activity.LoadingActivity
import cn.xz.zgsg.base.BaseDialogRxFragment
import cn.xz.zgsg.util.ARouter

/**
 * 应用过期弹窗
 * */
class LoseAppDialog: BaseDialogRxFragment() {

    private var mClickListener: OnClickListener? = null

    private var tvC: TextView? = null
    private var tvSure: TextView? = null
    private var ivTitle: ImageView? = null

    private var laterTime = false

    companion object {
        fun newInstance(
            later: Boolean = false,
            mClickListener: OnClickListener
        ): LoseAppDialog {
            val mDialog = LoseAppDialog()
            mDialog.laterTime = later
            mDialog.mClickListener = mClickListener
            return mDialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_app_lose
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 350)
    }

    override fun initViews() {

        dialog?.let {
            tvC = it.findViewById(R.id.tv_c)
            tvSure = it.findViewById(R.id.tv_sure)
            ivTitle = it.findViewById(R.id.iv_title)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //视图
        initUI()
    }

    private fun initUI(){
        if (laterTime){
            tvC?.text = "应用已过期,已限制使用"
        }else{
            tvC?.text = "未检测到数据，请下载更新数据"
        }
        tvSure?.setOnClickListener {
            dismiss()
            if (laterTime){
                mClickListener?.onF()
            }else{
                ARouter.to(LoadingActivity::class.java)
            }
        }
        ivTitle?.setOnClickListener {
            ARouter.to(LoadingActivity::class.java)
        }
    }

    interface OnClickListener {
        fun onF()
    }
}